package leetcode.problems;

import org.junit.Test;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by gmwang on 2018/7/25
 * 仅翻转字母
 */
public class _0827ReverseOnlyLetters {
    /**
     * Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions.
     * 仅翻转字母，除外的还在原位。
     * Example 1:
     * <p>
     * Input: "ab-cd"
     * Output: "dc-ba"
     * Example 2:
     * <p>
     * Input: "a-bC-dEf-ghIj"
     * Output: "j-Ih-gfE-dCba"
     * Example 3:
     * <p>
     * Input: "Test1ng-Leet=code-Q!"
     * Output: "Qedo1ct-eeLg=ntse-T!"
     * <p>
     * <p>
     * Note:
     * <p>
     * S.length <= 100
     * 33 <= S[i].ASCIIcode <= 122
     * S doesn't contain \ or "
     * /**
     *
     * @param S
     * @return
     */
    public String reverseOnlyLetters(String S) {
        StringBuilder sb = new StringBuilder();
        char[] chars = S.toCharArray();
        char[] res = new char[S.length()];
        Queue letters = new LinkedList<>();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] < 65 || (chars[i] > 90 && chars[i] < 97)) {
                res[i] = chars[i];
            }else {
                ((LinkedList) letters).addFirst(chars[i]);
            }
        }
        for (int i = 0; i < res.length; i++) {
            if(res[i] != 0 ) {
                ((LinkedList) letters).add(i,res[i]);
            }
        }
        for (int i = 0; i < letters.size(); i++) {
            sb.append(((LinkedList) letters).get(i));
        }
        return sb.toString();
    }

    @Test
    public void test() {
        String a = "ab-cd";
//        String a = "a-bC-dEf-ghIj"; //"j-Ih-gfE-dCba"
//        String a = "Test1ng-Leet=code-Q!"; //"Qedo1ct-eeLg=ntse-T!"
//        String a = "7_28]"; // "7_28]"
        String res = reverseOnlyLetters(a);
        System.out.println(res);
    }
}
